import request from '@/utils/request'
import {encryptedData} from '@/utils/encrypt'
import {loginRSA} from '@/config'

export interface SmsCodeVO {
  mobile: string
  scene: number
}

export interface SmsLoginVO {
  mobile: string
  code: string
}

// 登录
export const login = async (data: any) => {
  if (loginRSA) {
    data = await encryptedData(data)
  }
  return request({
    url: '/system/auth/logins',
    method: 'POST',
    data,
  })
}

// 刷新访问令牌
export const refreshToken = (refreshToken: string) => {
  return request({
    url: `/system/auth/refresh-token?refreshToken=${refreshToken}`,
    method: 'POST',
  })
}

// 使用租户名，获得租户编号
export const getTenantIdByName = (name: string, data: any) => {
  return request({
    url: `/system/tenant/get-id-by-name?name=${name}`,
    method: 'GET',
    data,
  })
}

// 使用租户域名，获得租户信息
export const getTenantByWebsite = (website: string, data: any) => {
  return request({
    url: `/system/tenant/get-by-website?website=${website}`,
    method: 'GET',
    data,
  })
}

// 登出
export const loginOut = (data: any) => {
  return request({
    url: '/system/auth/logout',
    method: 'POST',
    data,
  })
}

// 获取用户权限信息
export const getInfo = (data: any) => {
  return request({
    url: '/system/auth/get-permission-info-reform',
    method: 'GET',
    data,
  })
}

//获取登录验证码
export const sendSmsCode = (data: SmsCodeVO) => {
  return request({
    url: '/system/auth/send-sms-code',
    method: 'POST',
    data,
  })
}

// 短信验证码登录
export const smsLogin = (data: SmsLoginVO) => {
  return request({
    url: '/system/auth/sms-login',
    method: 'POST',
    data,
  })
}

// 社交快捷登录，使用 code 授权码
export function socialLogin(type: string, code: string, state: string) {
  return request({
    url: '/system/auth/social-login',
    method: 'POST',
    data: {
      type,
      code,
      state,
    },
  })
}

// 社交授权的跳转
export const socialAuthRedirect = (type: number, redirectUri: string) => {
  return request({
    url: `/system/auth/social-auth-redirect?type=${type}&redirectUri=${redirectUri}`,
    method: 'POST',
    data: {},
  })
}

// 获取验证图片以及 token
export const getCode = (data: any) => {
  return request({
    url: '/system/captcha/get',
    method: 'POST',
    data: data,
  })
}

// 滑动或者点选验证
export const reqCheck = (data: any) => {
  return request({
    url: '/system/captcha/check',
    method: 'POST',
    data: data,
  })
}
